<?php

/**
 * 三类占比统计
 * 
 * @version 1.0
 * @date 2014-02-07
 */
class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;

    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("account", $config);
    }

    public function __common() {
        parent::init();

        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;

            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';

            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    public function actionIndex() {

        $this->__common();

        $zExam = new zExam();
        $examArray = $zExam->_getExamObject($this->exam_id, $this->user_role_list);

        $this->title = '三类占比';
        $this->render('index', array(
            'examArray' => $examArray
        ));
    }

    public function actionList() {
        $this->__common();

        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/chart.css');
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/highcharts.js");
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/modules/exporting.js");
        Yii::app()->clientScript->registerScriptFile(THEME_JS_URL . "/chart.js");
        $this->layout = "//layouts/main_other";

        //项目对应的分数线
        $exam_id = $this->request->getParam('exam_id');
        $type = $this->request->getParam('type');

        $zExam = new zExam();
        $exam_array = $zExam->_getExamInfo($exam_id); //考试详细信息

        $zEdu = new ZEdu();
        $schoolObject = $zEdu->_getSchoolObject();  //学校信息

        $tableList2 = '';

        $photo_json = '';
        $photoSchool_num = $photoSchool_sbl = $photoSchool_mbl = $photoSchool_lbl = $photo_bl = array();  //图形显示数据
        
        $people_num = $zExam->_getExamStuPoint($exam_id);  //得到统计数据
        if (!empty($people_num)) {
            //默认按照占学校比例
            if ($type == 1) {
                //按照全区
                if (isset($people_num['school']) && !empty($people_num['school'])) {
                    $total_num = isset($people_num['total']) ? $people_num['total'] : 0;
                    foreach ($people_num['school'] as $key => $value) {

                        $sbn = $mbn = $lbn = 0;
                        $sbl = $mbl = $lbl = 0;

                        $sbn = isset($people_num['start_num'][$key]) ? $people_num['start_num'][$key] : 0;
                        $mbn = isset($people_num['more_num'][$key]) ? $people_num['more_num'][$key] : 0;
                        $lbn = isset($people_num['last_num'][$key]) ? $people_num['last_num'][$key] : 0;

                        if ($total_num > 0) {
                            $sbl = sprintf('%.2f', $sbn / $total_num)*100;   //前30
                            $mbl = sprintf('%.2f', $mbn / $total_num)*100;   //中40
                            $lbl = sprintf('%.2f', $lbn / $total_num)*100;   //后30
                        } else {
                            $sbl = 0;
                            $mbl = 0;
                            $lbl = 0;
                        }

                        $photoSchool_num[] = $schoolObject[$key];
                        $photoSchool_sbl[] = (float) $sbl;
                        $photoSchool_mbl[] = (float) $mbl;
                        $photoSchool_lbl[] = (float) $lbl;
                        $tableList2 .= '<tr height="38">
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $schoolObject[$key] . '</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $sbl . '%</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $mbl . '%</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $lbl . '%</span></td>
                                        </tr>';
                    }
                }
            } else {

                //按照学校
                if (isset($people_num['school']) && !empty($people_num['school'])) {
                    foreach ($people_num['school'] as $key => $value) {

                        $sbn = $mbn = $lbn = 0;
                        $sbl = $mbl = $lbl = 0;

                        $sbn = isset($people_num['start_num'][$key]) ? $people_num['start_num'][$key] : 0;
                        $mbn = isset($people_num['more_num'][$key]) ? $people_num['more_num'][$key] : 0;
                        $lbn = isset($people_num['last_num'][$key]) ? $people_num['last_num'][$key] : 0;

                        if (isset($people_num['school'][$key]) && $people_num['school'][$key] > 0) {
                            $sbl = sprintf('%.2f', $sbn / $people_num['school'][$key])*100;  //前30
                            $mbl = sprintf('%.2f', $mbn / $people_num['school'][$key])*100;  //中40
                            $lbl = sprintf('%.2f', $lbn / $people_num['school'][$key])*100;  //后30
                        } else {
                            $sbl = 0;
                            $mbl = 0;
                            $lbl = 0;
                        }

                        $photoSchool_num[] = $schoolObject[$key];
                        $photoSchool_sbl[] = (float) $sbl;
                        $photoSchool_mbl[] = (float) $mbl;
                        $photoSchool_lbl[] = (float) $lbl;
                        $tableList2 .= '<tr height="38">
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $schoolObject[$key] . '</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $sbl . '%</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $mbl . '%</span></td>
                                            <td align="center" class="border_d" valign="middle" width="100"><span style="padding:0 5px;">' . $lbl . '%</span></td>
                                        </tr>';
                    }
                }
            }
            $photo_bl = array(0=>$photoSchool_sbl,1=>$photoSchool_mbl,2=>$photoSchool_lbl);
        }

        $photo_json['dataNum'] = $photo_bl;
        $photo_json['categories'] = $photoSchool_num;
        $photo_json['type'] = array("前30%学生","中40%学生","后30%学生");
        $photo_json['lable'] = '人数';
        $photo_json['unit'] = '%';
        
        $this->title = '三类占比';
        $this->render('list', array(
            'exam_id'        => $exam_id,
            'type'           => $type,
            'tableList2'     => $tableList2,
            'exam_array'     => $exam_array,
            'photo_json'     => json_encode($photo_json)
        ));
    }
    
    public function actionImport_list() {
        $exam_id = $this->request->getParam('exam_id');
        $type = $this->request->getParam('type');

        if ($exam_id > 0) {
            $zExam = new zExam();
            $exam_array = $zExam->_getExamInfo($exam_id); //考试详细信息

            $zEdu = new ZEdu();
            $schoolObject = $zEdu->_getSchoolObject();  //学校信息

            $people_num = $zExam->_getExamStuPoint($exam_id);  //得到统计数据
            if (!empty($people_num)) {
                spl_autoload_unregister(array('YiiBase', 'autoload'));
                include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/extensions/PHPExcel.php');
                include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/extensions/PHPExcel/IOFactory.php');

                $objPHPExcel = new PHPExcel();
                $ktitle = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z');
            
                $text_list = '占全校比例';
                if($type==1){
                    $text_list = '占全区比例';
                }
                
                //第一行
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A1', $text_list)
                        ->setCellValue('B1', '前30%学生占比')
                        ->setCellValue('C1', '中40%学生占比')
                        ->setCellValue('D1', '后30%学生占比');
                
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
                
                //第二行
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . 2, '学校')
                        ->setCellValue('B' . 2, $exam_array['exam_name'])
                        ->setCellValue('C' . 2, $exam_array['exam_name'])
                        ->setCellValue('D' . 2, $exam_array['exam_name']);
                
                $ii = 3;
                //默认按照占学校比例
                if ($type == 1) {
                    //按照全区
                    if (isset($people_num['school']) && !empty($people_num['school'])) {
                        $total_num = isset($people_num['total']) ? $people_num['total'] : 0;
                        foreach ($people_num['school'] as $key => $value) {

                            $sbn = $mbn = $lbn = 0;
                            $sbl = $mbl = $lbl = 0;

                            $sbn = isset($people_num['start_num'][$key]) ? $people_num['start_num'][$key] : 0;
                            $mbn = isset($people_num['more_num'][$key]) ? $people_num['more_num'][$key] : 0;
                            $lbn = isset($people_num['last_num'][$key]) ? $people_num['last_num'][$key] : 0;

                            if ($total_num > 0) {
                                $sbl = sprintf('%.2f', $sbn / $total_num)*100;   //前30
                                $mbl = sprintf('%.2f', $mbn / $total_num)*100;   //中40
                                $lbl = sprintf('%.2f', $lbn / $total_num)*100;   //后30
                            } else {
                                $sbl = 0;
                                $mbl = 0;
                                $lbl = 0;
                            }
                            //第三行开始数据
                            $objPHPExcel->setActiveSheetIndex(0)
                                    ->setCellValue('A' . $ii, $schoolObject[$key])
                                    ->setCellValue('B' . $ii, $sbl)
                                    ->setCellValue('C' . $ii, $mbl)
                                    ->setCellValue('D' . $ii, $lbl);
                            
                            $ii++;
                        }
                    }
                } else {
                    //按照学校
                    if (isset($people_num['school']) && !empty($people_num['school'])) {
                        foreach ($people_num['school'] as $key => $value) {

                            $sbn = $mbn = $lbn = 0;
                            $sbl = $mbl = $lbl = 0;

                            $sbn = isset($people_num['start_num'][$key]) ? $people_num['start_num'][$key] : 0;
                            $mbn = isset($people_num['more_num'][$key]) ? $people_num['more_num'][$key] : 0;
                            $lbn = isset($people_num['last_num'][$key]) ? $people_num['last_num'][$key] : 0;

                            if (isset($people_num['school'][$key]) && $people_num['school'][$key] > 0) {
                                $sbl = sprintf('%.2f', $sbn / $people_num['school'][$key])*100;  //前30
                                $mbl = sprintf('%.2f', $mbn / $people_num['school'][$key])*100;  //中40
                                $lbl = sprintf('%.2f', $lbn / $people_num['school'][$key])*100;  //后30
                            } else {
                                $sbl = 0;
                                $mbl = 0;
                                $lbl = 0;
                            }
                            //第三行开始数据
                            $objPHPExcel->setActiveSheetIndex(0)
                                    ->setCellValue('A' . $ii, $schoolObject[$key])
                                    ->setCellValue('B' . $ii, $sbl)
                                    ->setCellValue('C' . $ii, $mbl)
                                    ->setCellValue('D' . $ii, $lbl);
                            
                            $ii++;
                        }
                    }
                }
                
                $objPHPExcel->setActiveSheetIndex(0);
                $file_nams = "三类占比";
                $file_nams .= ".xls";

                if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
                    $file_nams = urlencode($file_nams);
                    $file_nams = str_replace("+", "%20", $file_nams); // 替换空格
                }
                header("Content-Type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=" . $file_nams);
                header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
                header('Expires:0');
                header('Pragma:public');

                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                $objWriter->save('php://output');

                Yii::app()->end();
                spl_autoload_register(array('YiiBase', 'autoload'));
                
            } else {
                //没有相关信息
                echo 1;
                exit;
            }
        } else {
            //没有相关信息
            echo 1;
            exit;
        }
    }
    
}